package com.itheima.aspect;

import com.itheima.mapper.OperateLogMapper;
import com.itheima.pojo.OperateLog;
import com.itheima.util.CurrentHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;

import java.time.LocalDateTime;
import java.util.Arrays;

//@Aspect
//@Component
@Slf4j
public class OperationLogAspect {

    //@Autowired
    private OperateLogMapper operateLogMapper;


    @Around("execution(* com.itheima.controller.*.add*(..)) || execution(* com.itheima.controller.*.delete*(..)) || execution(* com.itheima.controller.*.update*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        Object proceed = pjp.proceed();
        long end = System.currentTimeMillis();

        OperateLog operateLog = new OperateLog();
        operateLog.setOperateEmpId(getCurrentUserId());
        operateLog.setOperateTime(LocalDateTime.now());
        operateLog.setClassName(pjp.getTarget().getClass().getName());
        operateLog.setMethodName(pjp.getSignature().getName());
        operateLog.setMethodParams(Arrays.toString(pjp.getArgs()));
        operateLog.setReturnValue(proceed.toString());
        operateLog.setCostTime(end - start);
        operateLogMapper.insert(operateLog);
        return proceed;
    }

    private int getCurrentUserId() {
        return CurrentHolder.getCurrentId();
    }
}
